package com.rivulet.foreign.service.impl;

import com.rivulet.foreign.mapper.UserMapper;
import com.rivulet.foreign.pojo.User;
import com.rivulet.foreign.pojo.UserInformation;
import com.rivulet.foreign.service.UserService;
import com.rivulet.foreign.tools.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLDataException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * {一句话形容此class功能}
 *
 * @author xujiac
 * @create 2021/6/3
 * @since 1.0.0
 */
@Service
public class UserServiceImpl implements UserService {
    final
    UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public String loginService(User user) {
        try {
            User relUser = userMapper.getUserById(user.getUser_id());
            if (
                    relUser != null &&
                            user.getPassword().equals(relUser.getPassword()) &&
                            user.getIdentity().equals(relUser.getIdentity())
            ) {
                return "100";
            } else if (relUser == null) {
                return "400";
            } else {
                return "401";
            }
        } catch (Exception e) {
            System.out.println(e);
            return "404";
        }
    }

    @Override
    public Results addUser(List<UserInformation> list) {
        Results results=new Results(404,null);
        List<String> troubles=new ArrayList<>();
        try {
            for(UserInformation info:list){
                if(userMapper.getUserById(info .getUser_id())!=null){
                    setInformation(info);
                    troubles.add("原"+info.getUser_id().toString()+"已覆盖！");
                    continue;
                }
                userMapper.setUser(info.getUser_id(),"123",1);
                userMapper.setUserInformation(info.getUser_id());
                setInformation(info);
            }
            results.setCode(100);
            results.setMsg(troubles.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return results;
    }

    @Override
    public Results setInformation(UserInformation information) {
        Results results = new Results(100, null);
        try {
            userMapper.updateUserInformation(
                    information.getPassport_name(),
                    information.getChinese_name(),
                    information.getStudent_type(),
                    information.getCitizenship(),
                    information.getSex(),
                    information.getBirth_date(),
                    information.getDormitory(),
                    information.getClasses(),
                    information.getStart_school_time(),
                    information.getLeave_school_time(),
                    information.getPassport_id(),
                    information.getPassport_end_time(),
                    information.getVisa_id(),
                    information.getVisa_end_time(),
                    information.getInsurance_end_time(),
                    information.getExpenditure_type(),
                    information.getMajor(),
                    information.getReligion(),
                    information.getWinning_information(),
                    information.getPhone_number(),
                    information.getWeChat(),
                    information.getEmail(),
                    information.getUser_id()
            );
            results.setMsg("信息更新成功！");
        } catch (Exception e) {
            System.out.println(e.toString());
            if(e.toString().startsWith("occurred while setting parameters")){
                results.setCode(401);
                results.setMsg("请限制表格中字段的长度，不要出现不合理的长度，例如手机号码不要超过11位，宿舍信息等基本信息不要超过255个字，");
            }else {
                results.setMsg("提交失败");
            }
        }
        return results;
    }

    @Override
    public Results getAllInformation() {
        Results<List> results = new Results(100, null);
        try {
            List<UserInformation> list = userMapper.getUserInformation();
            results.setData(list);
        } catch (Exception e) {
            results.setCode(404);
        }
        return results;
    }

    @Override
    public Results reSetPassword(User user, String password) {
        Results<Object> results = new Results(404, null);
        try {
            if (userMapper.getUserById(user.getUser_id()).getPassword().equals(user.getPassword())) {
                userMapper.reSetPassword(password, user.getUser_id());
            }
            results.setCode(100);
        } catch (Exception e) {
            results.setMsg("密码重置出错，请重试");
        }
        return results;
    }
}
